$(function(){
    // subnavbar
    $('.subnavbar').find('li').each(function(i){
		var mod = i % 3;
		if (mod === 2){
		    $(this).addClass('subnavbar-open-right');
		}
    });

    // Textarea maxlength
    $('textarea[maxlength]').maxlength({
		slider: true,
		maxCharacters: 160
	});

    // Loading images
    $('#load').hide();

    // Active/deactive categories/games
    $('a.active').click(function(){
		$('#load').fadeIn();

		var commentContainer = $(this).parent().parent();
		var type = $(this).attr('data-type');
		var id = $(this).attr('data-id');
		var value = $(this).attr('href');
		var data = 'mod=' + type + '&id='+ id + '&value=' + value;

		$.ajax({
		    type: "POST",
		    url: "/admin/active.php",
		    data: data,
		    cache: false,
		    success: function(data){
			 	$('#load').fadeOut();

			 	if (data == 1) {
			 	    $('a#active-' + id).html('<i class="icon-large icon-ok"></i>').attr('href', 0);
			 		commentContainer.css('background', '');
			 	} else {
			 		$('a#active-' + id).html('<i class="icon-large icon-remove"></i>').attr('href', 1);
			 		commentContainer.css('background', 'yellow');
			 	}
		    },
		    error: function(){
				$('#load').fadeOut();
				$('#load').html('<i class="icon-small icon-warning-sign"></i>');
		    }
		});

		return false;
    });

    // Popular categories/games
    $('a.popular').click(function(){
		$('#load').fadeIn();

		var type = $(this).attr('data-type');
		var id = $(this).attr('data-id');
		var value = $(this).attr('href');
		var data = 'mod=' + type + '&id='+ id + '&value=' + value;

		$.ajax({
		    type: "POST",
		    url: "/admin/popular.php",
		    data: data,
		    cache: false,
		    success: function(data){
			 	$('#load').fadeOut();

			 	if (data == 1)
			 	    $('a#popular-' + id).html('<i class="icon-large icon-star"></i>').attr('href', 0);
			 	else
			 	   $('a#popular-' + id).html('<i class="icon-large icon-star-empty"></i>').attr('href', 1);
		    },
		    error: function(){
				$('#load').fadeOut();
				$('#load').html('<i class="icon-small icon-warning-sign"></i>');
		    }
		});

		return false;
    });

    // Deletete categories
    $('a.cdelete').click(function(){
    	var answer = confirm ("Are you sure you want to delete from the database?");

    	if (answer) {
        	$('#load').fadeIn();

        	var commentContainer = $(this).parent().parent();
        	var id = $(this).attr('data-id');
        	var data = 'mod=category&id='+ id;

        	$.ajax({
        	    type: "POST",
        	    url: "/admin/delete.php",
        	    data: data,
        	    cache: false,
        	    success: function(){
	        		commentContainer.slideUp('slow', function() {$(this).remove();});
	        	 	$('#load').fadeOut();
        	    },
        	    error: function(){
	        		$('#load').fadeOut();
	        		$('#load').html('<i class="icon-small icon-warning-sign"></i>');
        	    }
        	});
    	}

    	return false;
    });
    
 // Deletete game
    $('a.gdelete').click(function(){
    	var answer = confirm ("Are you sure you want to delete from the database?");

    	if (answer) {
        	$('#load').fadeIn();

        	var commentContainer = $(this).parent().parent();
        	var id = $(this).attr('data-id');
        	var data = 'mod=games&id='+ id;

        	$.ajax({
        	    type: "POST",
        	    url: "/admin/delete.php",
        	    data: data,
        	    cache: false,
        	    success: function(){
	        		commentContainer.slideUp('slow', function() {$(this).remove();});
	        	 	$('#load').fadeOut();
        	    },
        	    error: function(){
	        		$('#load').fadeOut();
	        		$('#load').html('<i class="icon-small icon-warning-sign"></i>');
        	    }
        	});
    	}
    	return false;
    });

    // Check game
    $('#checkgame').click(function(){
    	var gname = $('#g_name').val();

    	if (gname) {
    		$('#load').fadeIn();

    		var data = 'gname='+ gname;

    		$.ajax({
    			type: "POST",
        	    url: "/admin/checkgame.php",
        	    data: data,
        	    cache: false,
        	    success: function(string){
	        		$('#result').html(string);
	        	 	$('#load').fadeOut();
        	    },
        	    error: function(){
	        		$('#load').fadeOut();
	        		$('#load').html('<i class="icon-small icon-warning-sign"></i>');
        	    }
    		});
    	}

    	return false;
    });

    // Add tags
    var $tagsInput = $('#text_tag_input');
    $('#tags_select input').click(function() {
    	$tagsInput[0].value += $(this).val() + ', ';
    });

    // 
    $('.copyText').click(function() {
    	if ($('#tmp').length) {
    		 $('#tmp').remove();
    	}
    	var clickText = $(this).text();
    	$('<textarea id="tmp" />').appendTo($(this)).val(clickText).focus().select();
        return false;
    });
});